package com.ufo.store.management.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.ufo.store.management.R;
import com.ufo.store.management.entity.PayMaster;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 *  作者：taoli on 2017/6/28 0028 10:36
 *  
 */
public class SaleDetailRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context = null;
    private List<PayMaster> mPayMasterInfoList = null;
    private int lastPosition = -1;

    public SaleDetailRecyclerViewAdapter(Context context) {
        this.context = context;
        this.mPayMasterInfoList = new ArrayList<PayMaster>();
    }

    public void insertData(List<PayMaster> data) {
        mPayMasterInfoList.addAll(data);
    }

    public void removeData(int pos) {
        mPayMasterInfoList.remove(pos);
    }

    public void setData(List<PayMaster> data) {
        mPayMasterInfoList.clear();
        mPayMasterInfoList.addAll(data);
    }

    public PayMaster getItem(int pos) {
        PayMaster info = mPayMasterInfoList.get(pos);
        return info;
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }

    @Override
    public int getItemCount() {
        return mPayMasterInfoList.size();
    }

    @Override
    public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        holder.itemView.clearAnimation();
    }

    /**
     * @desc 自定义动画
     * @author taoli
     * @time 2017/6/9 0009
     */
    private void setAnimation(View viewToAnimate, int position) {
        if (position > lastPosition) {
            Animation translateAnimation = new TranslateAnimation(0, 0, 100, 0);
            translateAnimation.setDuration(700);
            translateAnimation.setInterpolator(new OvershootInterpolator());
            viewToAnimate.setAnimation(translateAnimation);
            lastPosition = position;
        } else if (position < lastPosition) {
            Animation translateAnimation = new TranslateAnimation(0, 0, -100, 0);
            translateAnimation.setDuration(700);
            translateAnimation.setInterpolator(new OvershootInterpolator());
            viewToAnimate.setAnimation(translateAnimation);
            lastPosition = position;
        }
    }

    class RecyclerViewHolder extends RecyclerView.ViewHolder {
        TextView payMasterBillnoTv;
        TextView payMasterOperdateTv;
        TextView payMasterBlotoutTv;
        TextView payMasterTotalTv;
        TextView payMasterReceivableTv;

        public RecyclerViewHolder(View view) {
            super(view);
            payMasterBillnoTv = (TextView) view.findViewById(R.id.paymaster_billno_tv);
            payMasterOperdateTv = (TextView) view.findViewById(R.id.paymaster_operdate_tv);
//          payMasterBlotoutTv = (TextView) view.findViewById(R.id.paymaster_blotout_tv);
//          payMasterTotalTv = (TextView) view.findViewById(R.id.paymaster_total_tv);
            payMasterReceivableTv = (TextView) view.findViewById(R.id.paymaster_receivable_tv);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        if (viewType == 1) {
            holder = new RecyclerViewHolder(LayoutInflater.from(
                    this.context).inflate(R.layout.paymaster_listitem, parent,
                    false));
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final PayMaster payMasterInfo = mPayMasterInfoList.get(position);
        if (payMasterInfo != null) {
            if (holder instanceof RecyclerViewHolder) {
                RecyclerViewHolder holder1 = (RecyclerViewHolder) holder;
                holder1.payMasterBillnoTv.setText(payMasterInfo.payMasterBillno);
                SimpleDateFormat format = new SimpleDateFormat("MM月dd日");
                String dateStr = format.format(payMasterInfo.payMasterOperdate) + " " + payMasterInfo.payMasterOpertime;
                holder1.payMasterOperdateTv.setText(dateStr);
//            holder1.payMasterBlotoutTv.setText(String.valueOf(payMasterInfo.payMasterBlotout));
//            holder1.payMasterTotalTv.setText(String.valueOf(payMasterInfo.payMasterTotal));
                holder1.payMasterReceivableTv.setText(String.valueOf(payMasterInfo.payMasterTotal));
            }
        }

        //进入动画
        setAnimation(holder.itemView, position);

        //点击事件
        if (mListener != null) {
            /**
             * 这里加了判断，itemViewHolder.itemView.hasOnClickListeners()
             * 目的是减少对象的创建，如果已经为view设置了click监听事件,就不用重复设置了
             * 不然每次调用onBindViewHolder方法，都会创建两个监听事件对象，增加了内存的开销
             */
            if (!holder.itemView.hasOnClickListeners()) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getPosition();
                        mListener.onItemClick(v, pos);
                    }
                });
                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int pos = holder.getPosition();
                        mListener.onItemLongClick(v, pos);
                        return true;
                    }
                });
            }
        }
    }

    private OnClickListener mListener = null;

    public void setOnClickListener(OnClickListener l) {
        mListener = l;
    }

    public interface OnClickListener {
        void onItemClick(View v, int position);

        void onItemLongClick(View v, int position);
    }
}
